home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9104.ARJ / 9N04092A < prev    next >
Text File  |  1991-02-06  |  2KB  |  88 lines

  1.  
  2. // PROGRAM LISTING
  3.  
  4. #include <stdlib.h> // necessary include files
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. // *** DEFINE DATA STRUCTURES
  9.  
  10. struct studentEntry 
  11.     {
  12.     char name[40];
  13.     float grade;
  14.     } student [100];    // student is an array of studentEntry elements
  15.  
  16. char name[100][40]; //name is an array of 40-character elements
  17. float grade[100];   //grade is an array of float elements
  18.  
  19. // *** PROTOTYPE QSORT() COMPARE FUNCTIONS FOR EACH DATA TYPE
  20.  
  21. int nameComp(const void *, const void *);
  22. int gradeComp(const void *, const void *);
  23. int structComp(const void *, const void *);
  24.  
  25. // *** BEGIN MAIN PROGRAM
  26.  
  27. int main( int argc, char *argv[] ) 
  28.      {
  29.      // read student file (however it must be done)
  30.      FILE *studentFile;
  31.      if(!(studentFile=fopen(argv[1],"rt"))) 
  32.           {
  33.           printf("error opening input student file\n");
  34.           exit(1);
  35.           }
  36.      
  37.      // read studentFile
  38.      char line[100];
  39.      char tmpName[40];
  40.      float tmpGrade;
  41.      for( int i=0; fgets(line, 100, studentFile); i++ ) 
  42.           {
  43.           sscanf(line,"%s %f", tmpName, &tmpGrade);
  44.           grade[i]=student[i].grade=tmpGrade;
  45.           strcpy(name[i],strcpy(student[i].name,tmpName));
  46.          }
  47.  
  48.      fclose(studentFile);
  49.  
  50.      // *** SORT each list using its sort function
  51.      qsort((void *)student,i,sizeof(studentEntry),structComp);
  52.      qsort((void *)name,i,sizeof(char)*40,nameComp);
  53.      qsort((void *)grade,i,sizeof(float),gradeComp);
  54.  
  55.      // print results
  56.      for( int j=0; j<i; j++ )
  57.           printf ("%20s  %6.2f     %20s %6.2f\n", 
  58.               student[j].name,student[j].grade,name[j],grade[j]);
  59.  
  60.      }
  61.  
  62. // *** COMPARE FUNCTION DEFINITIONS
  63. // *** Notice the header.  In the main body of the function the
  64. // *** arguments are cast to what they represent.  The user must
  65. // *** do this1
  66.  
  67. int name Comp(const void *a, const void *b) 
  68.      {
  69.      return strcmp((char *)a,(char *)b);
  70.      }
  71.  
  72. int gradeComp(const void *a, const void *b) 
  73.      {
  74.      if(*((float *)a)<*((float *)b)) return -1;
  75.      if(*((float *)a)>*((float *)b)) return 1;
  76.      else return 0;
  77.      }
  78.  
  79. int structComp(const void *a, const void *b) 
  80.      {
  81.      if(((student Entry *)a)->grade<((studentEntry *)b)->grade) 
  82.          return -1;
  83.      if(((studentEntry *)a)->grade>((studentEntry *)b)->grade) 
  84.          return 1;
  85.      return 0;
  86.      }
  87.  
  88.